/**
 * Created by JetBrains WebStorm.
 * User: nhuang
 * Date: 12-4-12
 * Time: 上午10:48
 * To change this template use File | Settings | File Templates.
 */
;
(function ($) {
    $.fn.nPictureSlider = function (options) {
        var defaults = {
            delayTime:2000,
            fadeTime:500,
            event:'click',
//            controls:true,
//            preBtn:'PRE',
//            nextBtn:'NEXT',
//            firstBtn:'FIRST',
//            lastBtn:'LAST',
//            playBtn:'PLAT',
//            stopBtn:'STOP',
//            conBtn:'conBtn',
            autoEvent:true,
            haveTitle:true

        };
        var opts = $.extend(defaults, options);
        return  this.each(function () {
            var thisSliderBox = $(this),
                thisSlider = thisSliderBox.find(".slider"),
                picShow = thisSlider.find(".imgLists li"),
                picLists = picShow.length,
//                picWidth = picShow.width(),
//                picHeight = picShow.height(),
//                picTitle,
                curBtn,
                timer;

            createNumLists();

            function start() {
                if (!opts.autoEvent) {
                    curBtn = opts.playBtn
                } else {
                    curBtn = opts.stopBtn;
                    timer = setInterval(autoRun, opts.delayTime);
                }
            }

            function createNumLists() {
                var numListsHtml = "<div class='numLists'><ul><li class='numCur'>1</li>";
                for (var i = 2; i <= picLists; i++) {
                    numListsHtml += "<li>" + i + "</li>"
                }
                numListsHtml += "</ul></div>";
                thisSlider.append(numListsHtml);
            }

            function createTitle() {
                thisSlider.append("<div class='title'><div class='titleBg'></div><p class='titleText'>"+ picShow.eq(0).find("a").attr("title")+"</p></div>")
            }
            if (opts.haveTitle) {
            createTitle();
            }
//            function createControls() {
//                thisSliderBox.append("<div class='controlBox'></div>");
//                var controlHtml = "<a class='firstBtn'>" + opts.firstBtn + "</a><a class='preBtn'>" + opts.preBtn + "</a><a class='" + opts.conBtn + " playBtn'>" + curBtn + "</a><a class='nextBtn'>" + opts.nextBtn + "</a><a class='lastBtn'>" + opts.lastBtn + "</a> ";
//                $(".controlBox").html(controlHtml);
//            }

            function handAnimate(elem) {
                elem.addClass("numCur").siblings().removeClass("numCur");
                picShow.eq(elem.index()).fadeIn(opts.fadeTime).addClass("cur").siblings().removeClass("cur").hide();
                if (opts.haveTitle) {
                    var picTitle = picShow.eq(elem.index()).find("a").attr("title");
                    $(".titleText").html(picTitle)
                }
            }
            function autoRun() {
                var numItem,
                    firstItem = $(".numLists li").eq(0),
                    picCurrent = thisSlider.find(".imgLists li.cur"),
                    picIndex = picCurrent.index(),
                    numCurrent = $(".numLists li").eq(picIndex),
                    nextIndex = picIndex + 1;

                if (nextIndex === picLists) {
                    numItem = firstItem;
                } else {
                    numItem = numCurrent.next("li");
                }
                handAnimate(numItem);

            }

            if (opts.autoEvent) {
                start();
                thisSlider.hover(function () {
                    clearInterval(timer);
                }, function () {
                    start()
                })
            }

//            if (opts.controls) {
//                createControls();
//                $(".controlBox a").click(function () {
//                    clickBtn($(this))
//                });
//                function clickBtn(thisTar) {
//                    var tarBtn = thisTar.attr("class");
//                    switch (tarBtn) {
//                        case "firstBtn" :
//                            numItem = firstItem;
//                            autoRun();
//                            break;
//                        case "lastBtn":
//                            numItem = $(".numLists li").eq(picLists - 1);
//                            autoRun();
//                            break;
//                        case "preBtn":
//
//                            picCurrent = thisSlider.find(".imgLists li.cur");
//                            picIndex = picCurrent.index();
//                            numCurrent = $(".numLists li").eq(picIndex);
//                            nextIndex = picIndex + 1;
//                            numItem = numCurrent.prev("li");
//                            autoRun();
//                            break;
//                        case "nextBtn":
//                            numItem = numCurrent.next("li");
//                            autoRun();
//                            break;
//                        default :
//                            if (thisTar.hasClass("playBtn")) {
//                                opts.autoEvent = false;
//                                clearInterval(timer);
//                                thisTar.addClass("stopBtn").removeClass("playBtn").html(opts.playBtn);
//                            } else {
//                                opts.autoEvent = true;
//                                start();
//                                thisTar.addClass("playBtn").removeClass("stopBtn").html(opts.stopBtn);
//                            }
//                            break;
//                    }
//                }
//            }

            thisSlider.find(".numLists li").bind(opts.event, function () {
                handAnimate($(this))
            });
        })
    }
})(jQuery);
